//
//  ISlInformChangeCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/5.
//  Copyright © 2016年 game. All rights reserved.

#import "ISlInformChangeCell.h"
#import "InfmChangeCell.h"
#import "InformIdentityCell.h"
#define CellHeight SCREEN_HEIGHT*0.07
#define HeightForSectionFoot 12
#define tableViewHight (SCREEN_HEIGHT-64)-20

@interface ISlInformChangeCell ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong) UILabel *placeHolderLabel;//textView中的水印字体label
@property (nonatomic,assign) BOOL IsUpGo;
@property (nonatomic,strong) NSArray *titleAry;
@property (nonatomic,assign) NSInteger viewTag;

@end
@implementation ISlInformChangeCell

-(void)setIsYuJuDian:(BOOL)isYuJuDian{
    _isYuJuDian = isYuJuDian;
     [self.contentView addSubview:self.tableview];
}
-(NSArray *)titleAry{

    if (!_titleAry) {
    _titleAry = @[@"钓场名称",@"钓场位置",@"钓场类型",@"收费类型",@"有什么鱼",@"您的身份",@"联系人",@"钓场电话"];
        if (self.isYuJuDian) {
             _titleAry = @[@"渔具店名称",@"渔具店位置",@"渔具店类型",@"收费类型",@"有什么鱼",@"您的身份",@"联系人",@"渔具店电话"];
        }
    }
    return _titleAry;
}


-(void)setModel:(IslanInformChangeModel *)model{

    _model = model;

    self.textView.text = _model.desc;
    _placeHolderLabel.hidden = ![@"" isEqualToString:self.textView.text];
    [self.tableview reloadData];
}

-(UITextView *)textView{
    if (!_textView) {

        _textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-20, H(_tableview)/4)];

        if (self.isYuJuDian) {
            _textView.height = H(_tableview)/3;
        }
        
        _textView.tag=1618;
        _textView.font = font(SCREEN_WIDTH*0.045);
        _textView.delegate = self;
        _textView.returnKeyType = UIReturnKeyDone;

        //水印字体
        [_placeHolderLabel removeFromSuperview];
        _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, CGRectGetWidth(_textView.frame), 30)];
        _placeHolderLabel.textColor = [UIColor lightGrayColor];
        _placeHolderLabel.font = font(SCREEN_WIDTH*0.045);
        _placeHolderLabel.userInteractionEnabled = NO;
        _placeHolderLabel.text = @"在这里写垂钓的规则，垂钓时间，以及钓场的简介";
        _placeHolderLabel.hidden = ![@"" isEqualToString:self.textView.text];
        [_textView addSubview:_placeHolderLabel];
    }
    return _textView;
}

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, tableViewHight) style:UITableViewStylePlain];
        
        [_tableview registerNib:[UINib nibWithNibName:@"InfmChangeCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        [_tableview registerNib:[UINib nibWithNibName:@"InformIdentityCell" bundle:nil] forCellReuseIdentifier:@"idCell"];
        
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.scrollEnabled = NO;
        
        _tableview.backgroundColor = [UIColor clearColor];
         _tableview.tableFooterView = self.textView;
    }
    return _tableview;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
 
        self.backgroundColor = [UIColor clearColor];//RGBA(230, 230, 238, 1);
        self.selectionStyle = 0;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    return self;


}

- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    UIView *view = [self.contentView viewWithTag:self.viewTag];

        if (keyboardF.origin.y==SCREEN_HEIGHT) {
            
            [self.deleget keboardDown];
            
        }else{
        
                           [self.deleget keboardUpWhithView:view AndKeboardRect:keyboardF];
        
            
        }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section==0) {
        
        if (self.isYuJuDian) {
            return 2;
        }else{
            return 5;
        }
        
    }else{
        return 3;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-20, 8)];
    view.backgroundColor = RGBA(230, 230, 238, 1);
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return HeightForSectionFoot;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
     tableView.separatorStyle = 0;
    
    if (indexPath.section==1&&indexPath.row==0) {
        
        InformIdentityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idCell"];
        cell.dicKey = @"identity";
        cell.identity = _model.identity;
        cell.isYuJuDian = self.isYuJuDian;
        return cell;
    }else{
        
    InfmChangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.text.tag = indexPath.row+indexPath.section*5+100;
    cell.text.delegate = self;
    if (indexPath.section==0) {
        if (indexPath.row==0) {
               cell.imgBtn.alpha = 0;
        }else if(indexPath.row==1){
            [cell.imgBtn setImage:[UIImage imageNamed:@"钓点标注"] forState:UIControlStateNormal];
            [cell.imgBtn addTarget:self action:@selector(diaochangweizhi) forControlEvents:UIControlEventTouchUpInside];
         
        }else{
            cell.text.enabled = NO;
        }
    }
    
    if (indexPath.section==1) {
        cell.imgBtn.alpha = 0;
    }
    
        cell.title.text = self.titleAry[indexPath.row+indexPath.section*5];
    if (indexPath.section==0) {
        
        switch (indexPath.row) {
            case 0:
                cell.text.text = _model.name;
                cell.text.placeholder = @"请输入钓场名称";
                if (self.isYuJuDian) {
                    cell.text.placeholder = @"请输入渔具店名称";
                }
                cell.dicKey = @"name";
                break;
            case 1:
                cell.text.text = _model.location;
                cell.text.placeholder = @"请选择钓场位置";
                if (self.isYuJuDian) {
                    cell.text.placeholder = @"请选择渔具店位置";
                }
                cell.dicKey = @"location";
                break;
            case 2:
                cell.text.text = _model.type_detail;
                cell.dicKey = @"type_detail";
                break;
            case 3:
                cell.text.text = _model.charge_money;
                cell.dicKey = @"charge_money";
                break;
            case 4:
                cell.text.text = _model.fish_detail;
                cell.dicKey = @"fish_detail";
                break;
                
            default:
                break;
        }
        
    }else{
    
        switch (indexPath.row) {
           
            case 1:
                cell.text.text = _model.contacts;
                cell.dicKey = @"contacts";
                break;
            case 2:
                cell.text.text = _model.phone;
                cell.dicKey = @"phone";
                break;
            default:
                break;
        }
    
    }
    
    return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return CellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    
    InfmChangeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section==0) {
        if (indexPath.row==2) {
            
            [self.deleget DiaochangLeiXing:cell.text];
            
        }else if (indexPath.row==3){//收费类型
            
            [self.deleget ShoufeiLeixing:cell.text];
        
        }else if(indexPath.row==4){
            [self.deleget YouSmYu:cell.text];
        }
    }
}
-(void)diaochangweizhi{
    UITextField *textField = [self.contentView viewWithTag:101];
 [self.deleget NoLocalMesssageWhithTextField:textField];
}

#pragma mark - text view delegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [_textView resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView{
    _placeHolderLabel.hidden = ![@"" isEqualToString:textView.text];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

    self.IsUpGo = YES;
    self.viewTag = textView.tag;
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    if (self.number==30||self.number==40) {
    
        if (textField.tag==101) {
            if (textField.text.length==0) {//发布时 如果没有内容则提示则提示用户打开定位  如果打开了则跳转到地图 获取位置
                
                [self.deleget NoLocalMesssageWhithTextField:textField];
                return NO;
            }
        }
    }
    
    if (!self.isYuJuDian) {
        if (textField.tag>104) {
            self.IsUpGo = YES;
        }else{
            self.IsUpGo = NO;
        }
    }
    
    
   self.viewTag = textField.tag;
    return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

-(void)setTijiao:(NSString *)tijiao{
    _tijiao = tijiao;
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    UIView * allcellView = _tableview.subviews[0];

    for (InfmChangeCell * Cell in allcellView.subviews) {
        
        [dic addEntriesFromDictionary:@{Cell.dicKey:Cell.text.text}];
    }

    [dic addEntriesFromDictionary:@{@"desc":self.textView.text}];
    
    [self.deleget TijiaoWhithDic:dic];

}
@end